package com.hy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.domain.User;
import com.hy.domain.UserFriend;
import com.hy.domain.dto.FriendDTO;
import com.hy.domain.dto.UserDTO;
import com.hy.service.UserFriendService;
import com.hy.mapper.UserFriendMapper;
import com.hy.service.UserService;
import com.hy.util.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author hou
* @description 针对表【user_friend】的数据库操作Service实现
* @createDate 2023-06-08 15:52:31
*/
@Service
public class UserFriendServiceImpl extends ServiceImpl<UserFriendMapper, UserFriend>
    implements UserFriendService{

    @Resource
    private UserService userService;

    /**
     * 查询用户好友列表
     * @param userId
     * @return
     */
    @Override
    public List<UserDTO> listFriendsByUserId(Long userId) {
        final List<UserDTO> friendDTOS = this.baseMapper.selectFriendByUserId(userId);
        return friendDTOS;
    }

    /**
     * 添加好友
     * @param friendId
     */
    @Override
    public void addFriend(Long friendId) {
        final UserDTO user = UserHolder.getUser();
        final Integer count = userService.lambdaQuery()
                .eq(User::getUserId, user.getUserId()).count();
        // 用户存在
        if (count > 0) {
            final UserFriend userFriend = new UserFriend();
            userFriend.setUserId(user.getUserId());
            userFriend.setFriendId(friendId);
            userFriend.setFriendGroup(0);
            userFriend.setCreateTime(LocalDateTime.now());
            System.out.println("userFriend = " + userFriend);
            this.save(userFriend);
        }
    }

    /**
     * 删除好友
     * @param friendId
     */
    @Override
    public void deleteFriend(Long friendId) {
        final UserDTO user = UserHolder.getUser();
        lambdaUpdate()
                .eq(UserFriend::getUserId, user.getUserId())
                .eq(UserFriend::getFriendId, friendId)
                .remove();
    }

    @Override
    public List<UserFriend> searchByKeyword(String keyword) {
        return null;
    }

    @Override
    public List<Long> listIdsByUserId(Long userId) {
        this.baseMapper.listIdsByUserId(userId);
        return null;
    }
}




